import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

export default new Vuex.Store({
    state:{             //要共享出去的数据
        token:'',
        userInfo:JSON.parse(localStorage.getItem("userInfo")),//获取页面刷新后的状态
        gwc_number:'',
        orderlist:JSON.parse(localStorage.getItem("orderlist")),
        user_order:JSON.parse(localStorage.getItem("user_order")),
    },
    mutations:{
        SET_TOKEN: (state, token) => {
            state.token = token               //将token存入缓存
            localStorage.setItem("token", token)  
          },
          SET_USERINFO: (state, userInfo) => {  //将用户信息存入缓存
            state.userInfo =userInfo 
            localStorage.setItem("userInfo", JSON.stringify(userInfo))
          },
          SET_GWC:(state,gwc_number)=>{ //将用户购物车商品数量存入缓存
            state.gwc_number = gwc_number
            localStorage.setItem("gwc_number", JSON.stringify(gwc_number))
          },
          SET_dd:(state,orderlist)=>{ //将用户订单状态数量存入缓存
            state.orderlist = orderlist
            localStorage.setItem("order_list", JSON.stringify(orderlist))
          },
          SET_ORDER:(state,user_order)=>{ //将用户订单存入缓存
            state.user_order = user_order
            localStorage.setItem("user_order", JSON.stringify(user_order))
          },
          REMOVE_INFO: (state) => {       //退出清空缓存
            state.token = ''
            state.userInfo = {}
            state.gwc_number=''
            state.orderlist=[]
            state.user_order={}
            localStorage.removeItem("gwc_number")
            localStorage.removeItem("token")
            localStorage.removeItem("order_list")
            localStorage.removeItem("user_order")
            localStorage.removeItem("userInfo")
          }
    },
    getters: {
        getUser:state => {
            return state.userInfo
        },
        getGwc:state => {
          return state.gwc_number
        },
        getorder:state => {
          return state.orderlist
        },
        getuser_order:state => {
          return state.user_order
        }
    },
    actions: {
    },
    modules: {
    }
})